import os
import shutil

from .confirming import confirm
from .inputting import NoDefault


def confirm_overwrite_path(path: str, *, default: bool = NoDefault):
    """ Asks the user to confirm overwriting a may-be-existed path. """
    if not os.path.exists(path):
        return

    if os.path.isdir(path):
        confirm(f"Directory '{path}' already exists. Overwrite?", default=default)
        shutil.rmtree(path)
        print(f"Directory '{path}' deleted.")
    elif os.path.isfile(path):
        confirm(f"File '{path}' already exists. Overwrite?", default=default)
        os.remove(path)
        print(f"File '{path}' deleted.")
    else:
        raise ValueError(f"File '{path}' is neither a file nor a directory.")


def confirm_clear_file_path(path: str, *, default: bool = NoDefault):
    confirm_overwrite_path(path, default=default)
    os.makedirs(os.path.dirname(path), exist_ok=True)


def confirm_clear_dir_path(path: str, *, default: bool = NoDefault):
    confirm_overwrite_path(path, default=default)
    os.makedirs(path, exist_ok=True)
